package com.example.springbootmybatis.filter;

import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @title: FirstFilter
 * @Author Tan
 * @Date: 2021/6/5 20:23
 * @Version 1.0
 */
@Order(1)
@WebFilter(filterName = "firstFilter", urlPatterns = "/indexgame")
public class FirstFilter implements Filter {
    private static final String[] invalidCharacter = new String[]{
            "select", "insert", "delete", "update"
    };

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("first filter start!!!");
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpSession httpSession = httpServletRequest.getSession();
        Object user = httpSession.getAttribute("user");
        // 没有用户信息时进行请求转发
        if (user == null) {
            httpServletRequest.getRequestDispatcher("/index").forward(httpServletRequest, response);
            System.out.println("first filter failed!!!");
            System.out.println("请输入账户名和密码");
            return;
        }
        // 执行目标资源，放行
        chain.doFilter(request, response);
        System.out.println("first filter success!!!");
    }

    @Override
    public void destroy() {

    }
}
